﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Windows.Forms;
using OpenTK;
using OpenTK.Graphics.OpenGL;
using OpenTK.Input;
using System.IO;


namespace AirHockey
{
    public partial class MainForm : Form
    {
        #region Members
        private bool isLoaded;
        private ComponentManager componentManager;
        private GameTimer gameTimer;
        private bool paused = false;
        private int gameType = 0;
        private int player1_score = 0;
        private int player2_score = 0;
        #endregion

        #region Constructor
        public MainForm()
        {
            InitializeComponent();
        }
        #endregion

        #region Events
        private void fileToolStripMenuItem1_Click(object sender, EventArgs e)
        {
            NewGameDiag StartUp = new NewGameDiag();
            StartUp.ShowDialog();
            gameType = StartUp.GameType;
        }

        private void glControl_Resize(object sender, EventArgs e)
        {
            if (isLoaded)
            {
                SetupViewSpace();
            }
        }

        private void glControl_Paint(object sender, PaintEventArgs e)
        {
            Player1Label.Text = "Player 1: " + player1_score.ToString();
            Player2Label.Text = "Player 2: " + player2_score.ToString();
            componentManager.Update(gameTimer.GameTime);
        }

        private void glControl_Load(object sender, EventArgs e)
        {
            gameTimer = GameTimer.Instance;
            componentManager = new ComponentManager(glControl);
            SetupViewSpace();
            LoadResources();
            isLoaded = true;
            Application.Idle += (s, ev) =>
            {
                while (glControl.IsIdle)
                {
                    glControl.Invalidate();
                }
            };
        }
        #endregion

        #region Private Methods
        /// <summary>
        /// Responsible for loading and adding all content in the game
        /// </summary>
        private void LoadResources()
        {
            string executableName = Application.ExecutablePath;
            FileInfo executableFileInfo = new FileInfo(executableName);
            string executableDirectoryName = executableFileInfo.DirectoryName + "\\Models";

            //Model model = ObjParser.ParseObjFile(executableDirectoryName, "Table");
            //componentManager.Add(model);

            Model surface = ObjParser.ParseObjFile(executableDirectoryName, "PlayArea");
            componentManager.Add(surface);
        }

        private void SetupViewSpace()
        {
            componentManager.SetViewMatrix(Matrix4.LookAt(new Vector3(5.0f, 15.0f, 20.0f),Vector3.Zero, Vector3.UnitY));
            componentManager.SetProjectionMatrix(Matrix4.CreatePerspectiveFieldOfView((float)Math.PI/4,glControl.Width/glControl.Height,0.1f,1000f));
        }

        /// <summary>
        /// Responsible for unloading all content in the game
        /// </summary>
        private void UnloadResources()
        {

        }
        #endregion

        private void glControl_KeyDown(object sender, KeyEventArgs e)
        {
            if (paused)
                return;
            switch (e.KeyCode)
            {
                case Keys.NumPad8:
                    componentManager.Light.LightPosition = new Vector3(componentManager.Light.LightPosition.X, componentManager.Light.LightPosition.Y + 0.1f, componentManager.Light.LightPosition.Z);
                    break;
                case Keys.NumPad2:
                    componentManager.Light.LightPosition = new Vector3(componentManager.Light.LightPosition.X, componentManager.Light.LightPosition.Y - 0.1f, componentManager.Light.LightPosition.Z);
                    break;
                case Keys.NumPad4:
                    componentManager.Light.LightPosition = new Vector3(componentManager.Light.LightPosition.X+0.1f, componentManager.Light.LightPosition.Y, componentManager.Light.LightPosition.Z);
                    break;
                case Keys.NumPad6:
                    componentManager.Light.LightPosition = new Vector3(componentManager.Light.LightPosition.X-0.1f, componentManager.Light.LightPosition.Y, componentManager.Light.LightPosition.Z);
                    break;
                case Keys.NumPad1:
                    componentManager.Light.LightPosition = new Vector3(componentManager.Light.LightPosition.X, componentManager.Light.LightPosition.Y, componentManager.Light.LightPosition.Z+0.1f);
                    break;
                case Keys.NumPad0:
                    componentManager.Light.LightPosition = new Vector3(componentManager.Light.LightPosition.X, componentManager.Light.LightPosition.Y, componentManager.Light.LightPosition.Z-0.1f);
                    break;
                case Keys.W:
                    componentManager.Camera.ChangeCameraPosition(new Vector3(componentManager.Camera.Position.X,componentManager.Camera.Position.Y + 0.1f,componentManager.Camera.Position.Z));
                    componentManager.SetViewMatrix(componentManager.Camera.ViewMatrix);
                    break;
                case Keys.S:
                    componentManager.Camera.ChangeCameraPosition(new Vector3(componentManager.Camera.Position.X, componentManager.Camera.Position.Y - 0.1f, componentManager.Camera.Position.Z));
                    componentManager.SetViewMatrix(componentManager.Camera.ViewMatrix);
                    break;
                case Keys.A:
                    componentManager.Camera.ChangeCameraPosition(new Vector3(componentManager.Camera.Position.X-0.1f, componentManager.Camera.Position.Y, componentManager.Camera.Position.Z));
                    componentManager.SetViewMatrix(componentManager.Camera.ViewMatrix);
                    break;
                case Keys.D:
                    componentManager.Camera.ChangeCameraPosition(new Vector3(componentManager.Camera.Position.X+0.1f, componentManager.Camera.Position.Y, componentManager.Camera.Position.Z));
                    componentManager.SetViewMatrix(componentManager.Camera.ViewMatrix);
                    break;
                case Keys.O:
                    componentManager.Camera.ChangeCameraPosition(new Vector3(componentManager.Camera.Position.X, componentManager.Camera.Position.Y, componentManager.Camera.Position.Z-0.1f));
                    componentManager.SetViewMatrix(componentManager.Camera.ViewMatrix);
                    break;
                case Keys.L:
                    componentManager.Camera.ChangeCameraPosition(new Vector3(componentManager.Camera.Position.X, componentManager.Camera.Position.Y, componentManager.Camera.Position.Z+0.1f));
                    componentManager.SetViewMatrix(componentManager.Camera.ViewMatrix);
                    break;
                default:
                    break;
            }
        }

        private void exitToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        private void pauseToolStripMenuItem_Click(object sender, EventArgs e)
        {
            //toggle pause
            paused = !paused;
            if (paused)
            {
                pauseToolStripMenuItem.Text = "Resume";
            }
            else
            {
                pauseToolStripMenuItem.Text = "Pause";
            }
        }

        private void restartToolStripMenuItem_Click(object sender, EventArgs e)
        {
            //Reset the score and puck
        }


        private void circleToolStripMenuItem_Click(object sender, EventArgs e)
        {
            //load circle paddle
        }

        private void squareToolStripMenuItem_Click(object sender, EventArgs e)
        {
            //load square paddle
        }

        private void replayLastPointToolStripMenuItem_Click(object sender, EventArgs e)
        {
            // Replay the last point
        }

        private void leaderBoardToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Form1 LeaderBoard = new Form1();
            LeaderBoard.textBox1.Text = "a";
            LeaderBoard.textBox2.Text = "a";
            LeaderBoard.textBox3.Text = "a";
            LeaderBoard.textBox4.Text = "a";
            LeaderBoard.textBox5.Text = "a";
            LeaderBoard.textBox6.Text = "a";
            LeaderBoard.textBox7.Text = "a";
            LeaderBoard.textBox8.Text = "a";
            LeaderBoard.textBox9.Text = "a";
            LeaderBoard.textBox10.Text = "a";
            LeaderBoard.ShowDialog();
        }


        
    }
}
